//
//  LYChatSystemCell.swift
//  WeChat
//
//  Created by mac on 2018/8/27.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit

private let kChatInfoFont: UIFont = UIFont.systemFont(ofSize: 13)
private let kChatInfoLabelMaxWidth: CGFloat = UIScreen.ts_width - 40 * 2
private let kChatInfoLabelPaddingLeft: CGFloat = 8 //左右分别留出 8 像素的留白
private let kChatInfoLabelPaddingTop: CGFloat = 4 //上下分别留出 4 像素的留白
private let kChatInfoLabelMarginTop: CGFloat = 3 //距离顶部
private let kChatInfoLabelMarginBottom: CGFloat = 10 //距离底部

class LYChatSystemCell: UITableViewCell {

    var infomationLabel: LYChatSystemEdgeLabel! {
        didSet{
            infomationLabel.font = kChatInfoFont
            infomationLabel.labelEdge = UIEdgeInsetsMake(0, kChatInfoLabelPaddingLeft, 0, kChatInfoLabelPaddingLeft)
            infomationLabel.layer.cornerRadius = 4
            infomationLabel.layer.masksToBounds = true
            infomationLabel.font = kChatInfoFont
            infomationLabel.textColor = UIColor.white
            infomationLabel.backgroundColor = UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 0.6)
        }
    }
    
    var model: ChatModel?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        infomationLabel = LYChatSystemEdgeLabel()
        self.contentView.addSubview(infomationLabel)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func setCellContent(_ model:ChatModel){
        self.model = model
        self.infomationLabel.text = model.messageContent
    }
    
    override func layoutSubviews() {
        guard let model = self.model else {
            return
        }
        
        self.infomationLabel.ts_setFrameWithString(model.messageContent!, width: kChatInfoLabelMaxWidth)
        self.infomationLabel.width = self.infomationLabel.width + kChatInfoLabelPaddingLeft * 2 //左右的留白
        self.infomationLabel.height = self.infomationLabel.height + kChatInfoLabelPaddingTop * 2 //上下的留白
        self.infomationLabel.left = (UIScreen.ts_width - self.infomationLabel.width) / 2
        self.infomationLabel.top = kChatInfoLabelMarginTop
    }
    
    class func layoutHeight(_ model:ChatModel) -> CGFloat {
        if model.cellHeight != 0 {
            return model.cellHeight
        }
        var height: CGFloat = 0
        height += kChatInfoLabelMarginTop + kChatInfoLabelMarginTop
        let stringHeight: CGFloat = model.messageContent!.ts_heightWithConstrainedWidth(kChatInfoLabelMaxWidth, font: kChatInfoFont)
        height += stringHeight + kChatInfoLabelPaddingTop*2
        model.cellHeight = height
        
        return model.cellHeight
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
